package controllers

import (
	"github.com/stretchr/testify/assert"
	v1alpha12 "gitverse.ru/synapse/kubelatte/pkg/api/v1alpha1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/event"
	"testing"
)

func TestTemplateControllerManageEvents(t *testing.T) {

	template := v1alpha12.Template{
		ObjectMeta: metav1.ObjectMeta{
			Name:       "test_name",
			Namespace:  "test_namespace",
			Generation: 1,
		},
	}

	template1 := v1alpha12.Template{
		ObjectMeta: metav1.ObjectMeta{
			Generation: 0,
		},
	}

	r := TemplateReconciler{}
	p := r.manageEvents()

	// Create test
	assert.True(t, p.Create(event.CreateEvent{Object: &template}))

	// Update test
	assert.True(t, p.Update(event.UpdateEvent{ObjectNew: &template, ObjectOld: &template1}))
	assert.False(t, p.Update(event.UpdateEvent{ObjectNew: &template, ObjectOld: &template}))

	//Delete test
	assert.True(t, p.Delete(event.DeleteEvent{Object: &template}))
}

func TestTriggerControllerManageEvents(t *testing.T) {

	trigger := v1alpha12.Trigger{
		ObjectMeta: metav1.ObjectMeta{
			Name:       "test_name",
			Namespace:  "test_namespace",
			Generation: 1,
		},
	}

	trigger1 := v1alpha12.Trigger{
		ObjectMeta: metav1.ObjectMeta{
			Generation: 0,
		},
	}

	r := TriggerReconciler{}
	p := r.manageEvents()

	// Create test
	assert.True(t, p.Create(event.CreateEvent{Object: &trigger}))

	// Update test
	assert.True(t, p.Update(event.UpdateEvent{ObjectNew: &trigger, ObjectOld: &trigger1}))
	assert.False(t, p.Update(event.UpdateEvent{ObjectNew: &trigger, ObjectOld: &trigger}))

	//Delete test
	assert.True(t, p.Delete(event.DeleteEvent{Object: &trigger}))
}

func TestTriggerReconciler_HealthCheck(t *testing.T) {
	t.Run("test", func(t *testing.T) {
		r := &TriggerReconciler{
			initedFlag: false,
		}
		assert.Equalf(t, false, r.HealthCheck(), "HealthCheck()")
	})
}
